import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'
import { Layout, getParentLayout } from '@/utils/routerHelper'
import { useI18n } from '@/hooks/web/useI18n'

const { t } = useI18n()

export const constantRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard/workplace',
    name: 'Root',
    meta: {
      hidden: true
    }
  },
  {
    path: '/redirect',
    component: Layout,
    name: 'Redirect',
    children: [
      {
        path: '/redirect/:path(.*)',
        name: 'Redirect',
        component: () => import('@/views/Redirect/Redirect.vue'),
        meta: {}
      }
    ],
    meta: {
      hidden: true,
      noTagsView: true
    }
  },
  {
    path: '/login',
    component: () => import('@/views/Login/Login.vue'),
    name: 'Login',
    meta: {
      hidden: true,
      title: t('router.login'),
      noTagsView: true
    }
  },
  {
    path: '/404',
    component: () => import('@/views/Error/404.vue'),
    name: 'NoFind',
    meta: {
      hidden: true,
      title: '404',
      noTagsView: true
    }
  }
]

export const asyncRouterMap: AppRouteRecordRaw[] = [
  {
    path: '/dashboard',
    component: Layout,
    redirect: '/dashboard/workplace',
    name: 'Dashboard',
    meta: {
      title: '首页',
      icon: 'ant-design:dashboard-filled',
      alwaysShow: true
    },
    children: [
      {
        path: 'workplace',
        component: () => import('@/views/Dashboard/Workplace.vue'),
        name: 'Workplace',
        meta: {
          title: '工作台',
          noCache: true,
          affix: true
        }
      }
    ]
  },
  {
    path: '/wellctrl',
    component: Layout,
    redirect: '/wellctrl/well',
    name: 'WellCtrl',
    meta: {
      title: '压井模块',
      icon: 'ep:management',
      alwaysShow: true
    },
    children: [
      {
        path: 'well',
        component: () => import('@/views/WellCtrl/Well.vue'),
        name: 'Well',
        meta: {
          title: '重点关注井'
        }
      },
      {
        path: 'info',
        component: () => import('@/views/WellCtrl/Info.vue'),
        name: 'Info',
        meta: {
          title: '详情',
          noTagsView: true,
          noCache: true,
          hidden: true,
          showMainRoute: true,
          activeMenu: '/wellctrl/well'
        }
      }
    ]
  },
  {
    path: '/emergency',
    component: Layout,
    redirect: '/emergency/warehouse',
    name: 'Emergency',
    meta: {
      title: '井控装备',
      icon: 'ci:error',
      alwaysShow: true
    },
    children: [
      {
        path: 'warehouse',
        component: () => import('@/views/Emergency/Warehouse.vue'),
        name: 'Warehouse',
        meta: {
          title: '仓库管理'
        }
      },
      {
        path: 'material',
        component: () => import('@/views/Emergency/Material.vue'),
        name: 'Material',
        meta: {
          title: '物资管理'
        }
      },
      {
        path: 'category',
        component: () => import('@/views/Emergency/Category.vue'),
        name: 'Category',
        meta: {
          title: '物资分类管理'
        }
      },
      {
        path: 'stock',
        component: () => import('@/views/Emergency/Stock.vue'),
        name: 'Stock',
        meta: {
          title: '库存管理'
        }
      },
      {
        path: 'inbound',
        component: () => import('@/views/Emergency/Inbound.vue'),
        name: 'Inbound',
        meta: {
          title: '入库管理'
        }
      },
      {
        path: 'outbound',
        component: () => import('@/views/Emergency/Outbound.vue'),
        name: 'Outbound',
        meta: {
          title: '出库管理'
        }
      },
      {
        path: 'expert',
        component: () => import('@/views/Emergency/Expert.vue'),
        name: 'Expert',
        meta: {
          title: '专家管理'
        }
      }
    ]
  },
  {
    path: '/knowledge',
    component: Layout,
    redirect: '/Knowledge/List',
    name: 'Knowledge',
    meta: {
      title: '知识库',
      icon: 'ci:error',
      alwaysShow: true
    },
    children: [
      {
        path: 'list',
        component: () => import('@/views/Knowledge/List.vue'),
        name: 'List',
        meta: {
          title: '知识库'
        }
      },
      {
        path: 'manage',
        component: () => import('@/views/Knowledge/Manage.vue'),
        name: 'Manage',
        meta: {
          title: '知识库管理'
        }
      },
      {
        path: 'category',
        component: () => import('@/views/Knowledge/KCategory.vue'),
        name: 'KCategory',
        meta: {
          title: '分类管理'
        }
      },
      {
        path: 'piece',
        component: () => import('@/views/Knowledge/Piece.vue'),
        name: 'Piece',
        meta: {
          title: '知识碎片管理',
          noTagsView: true,
          noCache: true,
          hidden: true,
          showMainRoute: true,
          activeMenu: '/Knowledge/List'
        }
      },
      {
        path: 'viewer',
        component: () => import('@/views/Knowledge/Viewer.vue'),
        name: 'Viewer',
        meta: {
          title: '知识浏览',
          noTagsView: true,
          noCache: true,
          hidden: true,
          showMainRoute: true,
          activeMenu: '/Knowledge/List'
        }
      }
    ]
  },
  {
    path: '/error',
    component: Layout,
    redirect: '/error/404',
    name: 'Error',
    meta: {
      title: '错误',
      icon: 'ci:error',
      alwaysShow: true
    },
    children: [
      {
        path: '404-demo',
        component: () => import('@/views/Error/404.vue'),
        name: '404Demo',
        meta: {
          title: '404'
        }
      },
      {
        path: '403-demo',
        component: () => import('@/views/Error/403.vue'),
        name: '403Demo',
        meta: {
          title: '403'
        }
      },
      {
        path: '500-demo',
        component: () => import('@/views/Error/500.vue'),
        name: '500Demo',
        meta: {
          title: '500'
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  strict: true,
  routes: constantRouterMap as RouteRecordRaw[],
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export const resetRouter = (): void => {
  const resetWhiteNameList = ['Redirect', 'Login', 'NoFind', 'Root']
  router.getRoutes().forEach((route) => {
    const { name } = route
    if (name && !resetWhiteNameList.includes(name as string)) {
      router.hasRoute(name) && router.removeRoute(name)
    }
  })
}

export const setupRouter = (app: App<Element>) => {
  app.use(router)
}

export default router
